/*
 * Copyright (c) 2014. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package com.dc.gameserver.serverCore.service.battle.coreBattleLogic.sameCardType;

import com.dc.gameserver.baseConfig.Config;
import com.dc.gameserver.hardCode.CardType;
import com.dc.gameserver.serverCore.service.battle.GameFormula;
import com.dc.gameserver.serverCore.service.battle.coreBattleLogic.AbstractSameLogic;
import com.dc.gameserver.serverCore.service.cardModule.Card;
import com.dc.gameserver.serverCore.service.cardModule.CardBox;
import com.dc.gameserver.serverCore.service.cardModule.CardBoxManager;
import com.dc.gameserver.serverCore.service.character.GameCharacter;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 石头哥哥
 *         Date: 13-11-1</br>
 *         Time: 上午9:58</br>
 *         Package: Server.ServerCore.Service.fightProcess.sameCardType</br>
 *         注解：玩家都出 技能 4  逻辑处理
 *         都出技能牌逻辑
 *
 */
@Service
public class magickLogic extends AbstractSameLogic {

    /**
     * 通过spring注解方式初始化处理logic的实例化引用
     */
    @Override
    public void PostConstruct() {
        LOGICS[CardType.MAGIC_TYPE.getType()]=this;
    }




    /**
     * logic 处理
     * 双方同出技能牌时,双方技能都有效
     * @param p1 玩家1
     * @param p2 玩家2
     *
     */
    @Override
    public void logicHandle(GameCharacter p1,GameCharacter p2) {
        CardBox cardBox= CardBoxManager.getCardBoxByID(p1.getCardBoxID());
        if (Config.DEFAULT_VALUE.GAME_VALUE.rulePoint){
            List<Card> outMagicList1=p1.getOutCardsList();
            List<Card> outMagicList2=p2.getOutCardsList();
            int magicCount1=0,magicCount2=0;
            for (com.dc.gameserver.serverCore.service.cardModule.Card card:outMagicList1){
                magicCount1+=card.getCount();
            }
            for (com.dc.gameserver.serverCore.service.cardModule.Card card:outMagicList2){
                magicCount2+=card.getCount();
            }

            if (magicCount1>magicCount2){
                //p1 active ,p2 hurt
                int garms= GameFormula.grams(p1.getSkillfiveType(), p2.getSelfFiveType());
                int p2Hurt=0;  //p2 造成的伤害
                int p2Shield=p2.getCurrentShield();
//                int p2Blood=p2.getCurrentBlood();
                int p2Defence=p2.getDefence();
                int p1FiveTypeSkillValue=p1.getFiveTypeSkillValue();

                if (garms== 0x1){
                    p2Hurt=GameFormula.magicHurtWithGrams(p2Defence, p1FiveTypeSkillValue);
                }else if (garms== 0x2){
                    p2Hurt=GameFormula.magicHurtWithUnGrams( p2Defence, p1FiveTypeSkillValue);
                } else {
                    p2Hurt=GameFormula.magicHurtWithGramsed(p2Defence,p1FiveTypeSkillValue);
                }
                if (p2Hurt<=0){
                    p2Hurt=0;
                    p2Hurt= Config.DEFAULT_VALUE.GAME_VALUE.hurt;
                }
                //p1 法力值消耗
                int p1Magic=GameFormula.computeMagicUse(p1); // notify
                if (p1Magic!=0){
                    p1.setCurrentMagic(p1.getCurrentMagic()-p1Magic);
                }

//                if (p2Shield>p2Hurt){
//                    p2.setCurrentShield(p2Shield-p2Hurt);
//                } else {
//                    if (p2Shield==0){
//                        p2.setCurrentBlood(p2Blood-p2Hurt);
//                    }else {
//                        p2.setCurrentBlood(p2Blood+p2Shield-p2Hurt);
//                        p2.setCurrentShield(0);
//                    }
//                }
                p2.setCurrentShield(p2Shield-p2Hurt);
                cardBox.sendHurtRule(p2.getID(),p2Hurt,p1.getBattleSkillID());

                if (p2.getCurrentShield()<=0){
                    p2.setCurrentShield(0);
//                    p2.setCurrentBlood(0);
                    //被攻击掉血
                    cardBox.flushBloodAndMagic();
                    cardBox.notifyPkResult(p1);
                    CardBoxManager.clearPkResourceWithRoundOver(cardBox.getCardBoxID());
                } else {
                    cardBox.flushBloodAndMagic();
                    cardBox.resetRoundResource();
                }
            }else if (magicCount1<magicCount2){
                int garms= GameFormula.grams(p2.getSkillfiveType(), p1.getSelfFiveType());
                //p1 hurt ,p2 active
                int p1Hurt=0;  //p1 造成的伤害
                int p1Shield=p1.getCurrentShield();
//                int p1Blood=p1.getCurrentBlood();
                int p1Defence=p1.getDefence();
                int p2FiveTypeSkillValue=p2.getFiveTypeSkillValue();

                if (garms== 0x1){
                    p1Hurt=GameFormula.magicHurtWithGrams(p1Defence, p2FiveTypeSkillValue);
                }else if (garms== 0x2){
                    p1Hurt=GameFormula.magicHurtWithUnGrams(p1Defence, p2FiveTypeSkillValue);
                } else {
                    p1Hurt=GameFormula.magicHurtWithGramsed( p1Defence, p2FiveTypeSkillValue);
                }
                if (p1Hurt<=0){
                    p1Hurt=0;
                    p1Hurt= Config.DEFAULT_VALUE.GAME_VALUE.hurt;
                }
                //p2 法力值消耗
                int p2Magic=GameFormula.computeMagicUse(p2); // notify
                if (p2Magic!=0){
                    p2.setCurrentMagic(p2.getCurrentMagic()-p2Magic);
                }

//                if (p1Shield>p1Hurt){
//                    p1.setCurrentShield(p1Shield-p1Hurt);
//                } else {
//                    if (p1Shield==0){
//                        p1.setCurrentBlood(p1Blood-p1Hurt);
//                    }else {
//                        p1.setCurrentBlood(p1Blood+p1Shield-p1Hurt);
//                        p1.setCurrentShield(0);
//                    }
//                }
                p1.setCurrentShield(p1Shield-p1Hurt);
                cardBox.sendHurtRule(p1.getID(),p1Hurt,p2.getBattleSkillID());
                if (p1.getCurrentShield()<=0){
                    p1.setCurrentShield(0);
//                    p1.setCurrentBlood(0);
                    //被攻击掉血
                    cardBox.flushBloodAndMagic();
                    cardBox.notifyPkResult(p2);
                    CardBoxManager.clearPkResourceWithRoundOver(cardBox.getCardBoxID());
                } else {
                    //点数相同
                    cardBox.flushBloodAndMagic();
                    cardBox.resetRoundResource();
                }
            }else{
                cardBox.flushBloodAndMagic();
                cardBox.resetRoundResource();
            }
        }else {
            int garms= GameFormula.grams(p2.getSkillfiveType(), p1.getSelfFiveType());
            //p1 hurt ,p2 active
            int p1Hurt=0;  //p1 造成的伤害
            int p1Shield=p1.getCurrentShield();
//            int p1Blood=p1.getCurrentBlood();
            int p1Defence=p1.getDefence();
            int p2FiveTypeSkillValue=p2.getFiveTypeSkillValue();

            if (garms== 0x1){
                p1Hurt=GameFormula.magicHurtWithGrams(p1Defence, p2FiveTypeSkillValue);
            }else if (garms== 0x2){
                p1Hurt=GameFormula.magicHurtWithUnGrams(p1Defence, p2FiveTypeSkillValue);
            } else {
                p1Hurt=GameFormula.magicHurtWithGramsed(p1Defence, p2FiveTypeSkillValue);
            }
            if (p1Hurt<=0){
                p1Hurt=0;
                p1Hurt= Config.DEFAULT_VALUE.GAME_VALUE.hurt;
            }
            //p2 法力值消耗
            int p2Magic=GameFormula.computeMagicUse(p2); // notify
            if (p2Magic!=0){
                p2.setCurrentMagic(p2.getCurrentMagic()-p2Magic);
            }

//            if (p1Shield>p1Hurt){
//                p1.setCurrentShield(p1Shield-p1Hurt);
//            } else {
//                if (p1Shield==0){
//                    p1.setCurrentBlood(p1Blood-p1Hurt);
//                }else {
//                    p1.setCurrentBlood(p1Blood+p1Shield-p1Hurt);
//                    p1.setCurrentShield(0);
//                }
//            }
            p1.setCurrentShield(p1Shield-p1Hurt);
            cardBox.sendHurtRule(p1.getID(),p1Hurt,p2.getBattleSkillID());
            //p1 active ,p2 hurt
            garms= GameFormula.grams(p1.getSkillfiveType(), p2.getSelfFiveType());
            int p2Hurt=0;  //p2 造成的伤害
            int p2Shield=p2.getCurrentShield();
//            int p2Blood=p2.getCurrentBlood();
            int p2Defence=p2.getDefence();
            int p1FiveTypeSkillValue=p1.getFiveTypeSkillValue();

            if (garms== 0x1){
                p2Hurt=GameFormula.magicHurtWithGrams(p2Defence, p1FiveTypeSkillValue);
            }else if (garms== 0x2){
                p2Hurt=GameFormula.magicHurtWithUnGrams(p2Defence, p1FiveTypeSkillValue);
            } else {
                p2Hurt=GameFormula.magicHurtWithGramsed(p2Defence,p1FiveTypeSkillValue);
            }
            if (p2Hurt<=0){
                p2Hurt=0;
                p2Hurt= Config.DEFAULT_VALUE.GAME_VALUE.hurt;
            }
            //p1 法力值消耗
            int p1Magic=GameFormula.computeMagicUse(p1); // notify
            if (p1Magic!=0){
                p1.setCurrentMagic(p1.getCurrentMagic()-p1Magic);
            }

//            if (p2Shield>p2Hurt){
//                p2.setCurrentShield(p2Shield-p2Hurt);
//            } else {
//                if (p2Shield==0){
//                    p2.setCurrentBlood(p2Blood-p2Hurt);
//                }else {
//                    p2.setCurrentBlood(p2Blood+p2Shield-p2Hurt);
//                    p2.setCurrentShield(0);
//                }
//            }
            p2.setCurrentShield(p2Shield-p2Hurt);
            cardBox.sendHurtRule(p2.getID(),p2Hurt,p1.getBattleSkillID());
            //p1 win
            if (p2.getCurrentShield()<=0){
                p2.setCurrentShield(0);
//                p2.setCurrentBlood(0);
                //被攻击掉血
                cardBox.flushBloodAndMagic();
                cardBox.notifyPkResult(p1);
                CardBoxManager.clearPkResourceWithRoundOver(cardBox.getCardBoxID());
                return;
            }

            //p2 win
            if (p1.getCurrentShield()<=0){
                p1.setCurrentShield(0);
//                p1.setCurrentBlood(0);
                //被攻击掉血
                cardBox.flushBloodAndMagic();
                cardBox.notifyPkResult(p2);
                CardBoxManager.clearPkResourceWithRoundOver(cardBox.getCardBoxID());
                return;
            }
            cardBox.flushBloodAndMagic();
            cardBox.resetRoundResource();
        }
    }
}
